/**
 * Copyright (C), 2015-2019, bogewang
 * FileName: IndexController
 * Author:   foren236598164@aliyun.com
 * Date:     2019/9/18 14:05
 * Description: 门户首页
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.bogewang.portal.controller;

import com.bogewang.core.base.BaseResponse;
import com.bogewang.member.entity.UserDTO;
import com.bogewang.member.feign.MemberServiceFeign;
import com.bogewang.web.base.BaseWebController;
import com.bogewang.web.constants.WebConstants;
import com.bogewang.web.utils.CookieUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 〈门户首页〉
 *
 * @author foren236598164@aliyun.com
 * @create 2019/9/18
 * @since 1.0.0
 */
@Controller
public class IndexController extends BaseWebController {

    /**
     * 跳转index首页;
     */
    private static final String INDEX_FTL = "index";
    @Autowired
    private MemberServiceFeign memberServiceFeign;

    /**
     * 跳转到首页
     *
     * @return
     */
    @RequestMapping("/")
    public String index(HttpServletRequest request, HttpServletResponse response, Model model) {
        String token = CookieUtils.getCookieValue(request, WebConstants.LOGIN_TOKEN_COOKIENAME, true);
        if (!isBlank(token)){
            BaseResponse<UserDTO> userInfo = memberServiceFeign.getInfo(token);
            if (isSuccess(userInfo)){
                UserDTO userDTO = userInfo.getData();
                if (userDTO != null){
                    String mobile = userDTO.getMobile();
                    String desensMobile = mobile.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
                    model.addAttribute("desensMobile", desensMobile);
                }
            }
        }

        return INDEX_FTL;
    }

    // 作业题：完成退出功能 实现唯一登陆心跳检测 前端定时器 定时 使用js 读取本地cookie 信息 使用token 查询当前状态 如果token状态为1的话，页面直接刷新下。

}